<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>语言列表</title>
    <link rel="stylesheet" href="../../../../component/pear/css/pear.css"/>
    <link rel="stylesheet" href="../../../../admin/css/reset.css"/>
    <style>
        .active {
            border-color: #57a3f3;
            color: #57a3f3;
        }

        .dateButtons {
            margin-bottom: 15px;
        }
    </style>
</head>

<body class="pear-container">
<!-- 数据表格 -->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="data-table" lay-filter="data-table" class="my-table"></table>
    </div>
</div>


<!-- 表格顶部工具栏 -->
<script type="text/html" id="table-toolbar">

</script>


<!-- 表格行工具栏 -->
<script type="text/html" id="table-bar">
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.user.update">编辑</button>
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.user.delete">删除</button>
</script>


<script src="../../../../component/layui/layui.js"></script>
<script src="../../../../component/pear/pear.js"></script>
<script src="../../../../lms/base64.js"></script>
<script src="../../../../lms/common.js"></script>
<script src="../../../../lms/permission.js"></script>
<script src="../../../../lms/config.js"></script>

<script>
    // 相关常量
    const PRIMARY_KEY = "tablename";
    const SELECT_API = lms_config.api_url + "/adminapi/system/backup/read";
    const UPDATE_API = lms_config.api_url + "/adminapi/system/database/update_mark";

    layui.use(['table', 'form', 'jquery', 'common', 'util', 'encrypt', 'notice'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let util = layui.util;
        let encrypt = layui.encrypt;
        let notice = layui.notice;

        let cols = [
            {
                title: '字段名',
                field: 'COLUMN_NAME',
                align: 'left'
            },
            {
                title: '数据类型',
                field: 'COLUMN_TYPE',
                align: 'left',
            },
            {
                title: '默认值',
                field: 'engine',
                align: 'left',
            },
            {
                title: '允许非空',
                field: 'IS_NULLABLE',
                align: 'left',
            },
            {
                title: '自动增递',
                field: 'update_time',
                align: "left",
            },
            {
                title: '备注',
                field: 'COLUMN_COMMENT',
                align: "left",
                edit: true,
                width: 200
            }
        ]

        window.render = function () {
            table.render({
                elem: "#data-table",
                url: SELECT_API,
                page: false,
                cols: [cols],
                skin: "line",
                where: {[PRIMARY_KEY]: getQueryString(PRIMARY_KEY)},
                toolbar: false,
                autoSort: false,
                parseData: function (ret) {
                    return {
                        "code": ret.status == 200 ? 0 : -1, // 解析接口状态
                        "msg": ret.msg, // 解析提示文本
                        "count": ret.data.count, // 解析数据长度
                        "data": ret.data.list // 解析数据列表
                    };
                },
                defaultToolbar: [{
                    title: "刷新",
                    layEvent: "refresh",
                    icon: "layui-icon-refresh",
                }, "filter", "print", "exports"]
            });
        }

        // 触发单元格工具事件
        table.on('edit(data-table)', function (obj) { // 双击 toolDouble
            let field = obj.field; // 得到字段
            let value = obj.value; // 得到修改后的值
            let data = obj.data; // 得到所在行所有键值
            if (field === 'COLUMN_COMMENT') {
                let list = {
                    field: data.COLUMN_NAME,
                    is_field: 1,
                    mark: data.COLUMN_COMMENT,
                    table: data.TABLE_NAME,
                    type: data.COLUMN_TYPE
                }
                $.ajax({
                    url: UPDATE_API,
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(list),
                    dataType: 'json',
                    success: function (ret) {
                        if (ret.code !== -1) {
                            notice.success(ret.msg)
                        } else {
                            return obj.reedit();
                        }
                    },
                    error: function (ret) {
                        notice.error("出错" + ret.status + "：" + ret.responseText)
                        return obj.reedit();
                    }
                })
            }
        });


        window.render();
    });


</script>
